﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Linq;

using SPU.DataAccess;
using SPU.Entities;

public partial class DataMaster_OrganisasiList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label_Title.Text = Page.Title;

        if (!IsPostBack)
        {
            PopulateNodes();
        }
    }

    #region Databind
    public void PopulateNodes()
    {
        TreeView_Main.Nodes.Clear();

        DataTable data = GetTreeViewData();
        DataView child = GetParent(data);
        foreach (DataRowView row in child)
        {
            TreeNode childNode = new TreeNode();
            childNode.Text = row["OrganisasiNama"].ToString();
            childNode.Value = row["OrganisasiID"].ToString();
            TreeView_Main.Nodes.Add(childNode);
            AddChild(data, childNode);
        }

        TreeView_Main.ExpandAll();
    }

    protected DataTable GetTreeViewData()
    {
        SPUDataAccess db = new SPUDataAccess();
        DataTable dt = new DataTable();
        DataRow dr;

        dt.Columns.Add("OrganisasiID");
        dt.Columns.Add("OrganisasiInduk");
        dt.Columns.Add("OrganisasiNama");

        var data = db.SPUDataContext.Organisasis.Select(p => p);
        foreach (Organisasi org in data)
        {

            dr = dt.NewRow();
            dr["OrganisasiID"] = org.OrganisasiID;
            dr["OrganisasiInduk"] = org.OrganisasiInduk;
            dr["OrganisasiNama"] = org.OrganisasiNama;
            dt.Rows.Add(dr);
        }

        return dt;
    }

    DataView GetParent(DataTable parent)
    {
        DataView view = new DataView(parent);
        view.RowFilter = "OrganisasiInduk='0'";
        return view;
    }

    DataView GetChild(DataTable parent, string parentID)
    {
        DataView view = new DataView(parent);
        view.RowFilter = "OrganisasiInduk='" + parentID + "'";
        return view;
    }

    void AddChild(DataTable parent, TreeNode node)
    {
        DataView child = GetChild(parent, node.Value);
        foreach (DataRowView row in child)
        {
            TreeNode childNode = new TreeNode();
            childNode.Text = row["OrganisasiNama"].ToString();
            childNode.Value = row["OrganisasiID"].ToString();
            node.ChildNodes.Add(childNode);
            AddChild(parent, childNode);
        }
    }
    #endregion Databind

    #region Action
    protected void TreeView_Main_SelectedNodeChanged(object sender, EventArgs e)
    {
        Button_Edit.Visible = true;
        Button_Update.Visible = false;
        Button_Save.Visible = false;
        Button_AddChild.Visible = true;
        MultiView_Content.SetActiveView(View_Detail);

        string id = TreeView_Main.SelectedValue;

        SPUDataAccess db = new SPUDataAccess();
        Organisasi org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(id)).Single<Organisasi>();

        Label_Value_ID.Text = org.OrganisasiID.ToString();
        Label_Value_Induk.Text = org.OrganisasiInduk.ToString();
        Label_Value_Nama.Text = org.OrganisasiNama;
        Label_Value_Keterangan.Text = org.Keterangan;

        UpdatePanel_Panel_Modal.Update();
        this.MPE.Show();
    }

    protected void ImageButton_Delete_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (TreeView_Main.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node in TreeView_Main.CheckedNodes)
                {
                    SPUDataAccess db = new SPUDataAccess();
                    Organisasi org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(node.Value)).Single<Organisasi>();
                    db.SPUDataContext.Organisasis.DeleteOnSubmit(org);
                    db.SPUDataContext.SubmitChanges();
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }

        PopulateNodes();
        UpdatePanel_Main.Update();
    }

    protected void ImageButton_Close_Click(object sender, ImageClickEventArgs e)
    {
        this.MPE.Hide();
        PopulateNodes();
        UpdatePanel_Main.Update();
    }

    protected void Button_Edit_Click(object sender, EventArgs e)
    {
        Button_Edit.Visible = false;
        Button_Update.Visible = true;
        Button_Save.Visible = false;
        Button_AddChild.Visible = false;
        TextBox_Form_ID.Enabled = false;
        TextBox_Form_Induk.Enabled = false;
        MultiView_Content.SetActiveView(View_Form);

        string id = TreeView_Main.SelectedValue;
        SPUDataAccess db = new SPUDataAccess();
        Organisasi org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(id)).Single<Organisasi>();

        TextBox_Form_ID.Text = org.OrganisasiID.ToString();
        TextBox_Form_Induk.Text = org.OrganisasiInduk.ToString();
        TextBox_Form_Nama.Text = org.OrganisasiNama;
        TextBox_Form_Keterangan.Text = org.Keterangan;
    }

    protected void Button_Update_Click(object sender, EventArgs e)
    {
        Button_Edit.Visible = true;
        Button_Update.Visible = false;
        Button_Save.Visible = false;
        Button_AddChild.Visible = true;
        MultiView_Content.SetActiveView(View_Detail);

        string id = TreeView_Main.SelectedValue;
        SPUDataAccess db = new SPUDataAccess();
        Organisasi org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(id)).Single<Organisasi>();

        org.OrganisasiNama = TextBox_Form_Nama.Text;
        org.Keterangan = TextBox_Form_Keterangan.Text;

        db.SPUDataContext.SubmitChanges();

        org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(id)).Single<Organisasi>();
        Label_Value_ID.Text = org.OrganisasiID.ToString();
        Label_Value_Induk.Text = org.OrganisasiInduk.ToString();
        Label_Value_Nama.Text = org.OrganisasiNama;
        Label_Value_Keterangan.Text = org.Keterangan;
    }

    protected void Button_Save_Click(object sender, EventArgs e)
    {
        SPUDataAccess db = new SPUDataAccess();
        Organisasi org = new Organisasi();

        org.OrganisasiID = TextBox_Form_ID.Text;
        org.OrganisasiInduk = TreeView_Main.SelectedValue;
        org.OrganisasiNama = TextBox_Form_Nama.Text;
        org.Keterangan = TextBox_Form_Keterangan.Text;

        db.SPUDataContext.Organisasis.InsertOnSubmit(org);
        db.SPUDataContext.SubmitChanges();

        this.MPE.Hide();
        PopulateNodes();
        UpdatePanel_Panel_Modal.Update();
        UpdatePanel_Main.Update();
    }

    protected void Button_AddChild_Click(object sender, EventArgs e)
    {
        Button_Edit.Visible = false;
        Button_Update.Visible = false;
        Button_Save.Visible = true;
        Button_AddChild.Visible = false;
        TextBox_Form_ID.Enabled = true;
        TextBox_Form_Induk.Enabled = true;
        MultiView_Content.SetActiveView(View_Form);

        TextBox_Form_ID.Text = String.Empty;
        TextBox_Form_Induk.Text = TreeView_Main.SelectedValue;
        TextBox_Form_Nama.Text = String.Empty;
        TextBox_Form_Keterangan.Text = String.Empty;
    }
    #endregion Action
}